// pages/login/login.js



const mdCode = require('../../utils/md5.js');
const desCode = require('../../utils/aesUtil.js');
const api_js = require("../../utils/api.js");
var app = getApp();



Page({

  /**
   * 页面的初始数据
   */
  data: {
    bar_Height: wx.getSystemInfoSync().statusBarHeight,
    // 手机号
    phone: "",
    // 邀请码
    inviteCode: "",
    // 438633
    // 短信验证码
    noteCode: "",


    errorPhone: false,
    errorInviteCode: false,
    errorNoteCode: false,

    sendCode: "发送",

    //是否能点击获取验证码的状态判断
    get_code_status: true, 
    get_code_color: false,
    show_get_code: '发送',
    get_code_time: 60,
    disalog: "none",
    flag: '',
    openid: '',
    avatarUrl: '',
    isShare: 0,


  },

  // 号码验证(手机)
  checkPhoness(phone) {

    if (!/^1[34578]\d{9}$/.test(phone)) {

      this.setData({
        errorPhone: true
      });
      return false;
    } else {
      this.setData({
        errorPhone: false
      });
      return true;
    }
  },


  checkPhoness2(phone) {

    if (!/^1[34578]\d{9}$/.test(phone)) {

      if(phone.length == 0 || phone.length == 11) {
        this.setData({
          errorPhone: true
        });
        return false;
      }

    } else {
      this.setData({
        errorPhone: false
      });
      return true;
    }
  },


  // 邀请码验证
  checkInviteCode(value) {
    if(value == '') {
      this.setData({
        errorInviteCode: true
      });
      return false;
    } else {
      this.setData({
        errorInviteCode: false
      });
      return true;
    }
  },

  // 短信验证
  checkNoteCode(value) {
    if(value == '') {
      this.setData({
        errorNoteCode: true
      });
      return false;
    } else {
      this.setData({
        errorNoteCode: false
      });
      return true;
    }
  },

  // 赋值和判断(手机号)
  phoneFun(e) {
    this.setData({
      phone: e.detail.value.replace(/\s+/g, '')
    });
    this.checkPhoness2(this.data.phone);
  },
  // 赋值和判断(邀请码)
  inviteCodeFun(e) {
    // return;
    this.setData({
      inviteCode: e.detail.value.replace(/\s+/g, '')
    });
    this.checkInviteCode(this.data.inviteCode);
  },
  // 赋值和判断(短信验证码)
  noteCodeFun(e) {
    this.setData({
      noteCode: e.detail.value.replace(/\s+/g, '')
    });
    this.checkNoteCode(this.data.noteCode);
  },


  // 发送短信验证码
  sendCodeFun() {
    var phone = this.data.phone;
    var wMsgID = desCode.wMsgID();
    if (this.checkPhoness(phone)) {
      if (this.data.get_code_status) {

        var that = this;
        var wParam = desCode.to3des('phone=' + phone);
        var md = mdCode.hexMD5('9999' + '103' + wMsgID + wParam + 'q1w2e3r4t5y6');
        const data = {
          wAgent: 9999,
          wAction: 103,
          wMsgID: wMsgID,
          wParam: wParam,
          wSign: md,
          wImei: 222,
          wVersion: 2,
          wRequestUserID: 4
        }
        api_js.postReq(data, (res) => {
          console.log(res, "验证码");
          // if (res.ReturnCode == 204) {
          //   wx.showToast({
          //     title: '手机号格式不正确',
          //     icon: 'none',
          //     duration: 3000
          //   })
          // }
          // 已发送50s
          if (res.ReturnCode == 0) {
            that.setData({
              show_get_code: '已发送' + (that.data.get_code_time) + 's',
              get_code_color: true,
              get_code_status: false
            });
  
            var timer = setInterval(function() {
              if (that.data.get_code_time > 0) {
                that.setData({
                  get_code_time: that.data.get_code_time - 1,
                  show_get_code: '已发送' + (that.data.get_code_time - 1) + 's',
                  get_code_color: true,
                  get_code_status: false
                });
              } else {
                clearInterval(timer);
                that.setData({
                  get_code_time: 60,
                  show_get_code: '重新发送',
                  get_code_color: false,
                  get_code_status: true
                });
              }
            }, 1000);
          }
  
          that.setData({
            // login_code: res.data.data.code //后台返回的验证码，可以做判断用
          });
  
        })
      }

      }

    
  },








  // 表单
  formSubmit() {
    var that = this;

    this.checkPhoness(this.data.phone);
    this.checkInviteCode(this.data.inviteCode);
    this.checkNoteCode(this.data.noteCode);
    if(this.checkPhoness(this.data.phone) && this.checkInviteCode(this.data.inviteCode) && this.checkNoteCode(this.data.noteCode)) {

      console.log(this.data.phone);
      console.log(this.data.inviteCode);
      console.log(this.data.noteCode);
      
      // 邀请码 438633


      // return
    //   入参：
    //   phone ///手机号
    // invitationcode ///邀请码
    // code ///短信验证码
    // openid ///openid


      // app.globalData.LoginStatus

      var wMsgID = desCode.wMsgID();
      // console.log('_openid', that.data.openid);
      var wParam = desCode.to3des(`phone=${that.data.phone}_invitationcode=${that.data.inviteCode}_code=${that.data.noteCode}_openid=${app.globalData.openid}`);
      var md = mdCode.hexMD5('9999' + '102' + wMsgID + wParam + 'q1w2e3r4t5y6');
      const data = {
        wAgent: 9999,
        wAction: 102,
        wMsgID: wMsgID,
        wParam: wParam,
        wSign: md,
        wImei: 222,
        wVersion: 2,
        wRequestUserID: 4
      }
      api_js.postReq(data, (res) => {
        
       
        if (res.ReturnCode == 202) {
          wx.showToast({
            title: '邀请码不正确',
            icon: 'none',
            duration: 2000,
            mask: true,
          })
        }
        if (res.ReturnCode == 203) {
          wx.showToast({
            title: '验证码不正确',
            icon: 'none',
            duration: 2000,
            mask: true,
          })
        }
  
        if (res.ReturnCode == 0) {


          console.log(res, "登录转态");

          // 1:参会人员 2:扫码人员
          // app.globalData.LoginStatus = res.Data[0].loginstatus;
          console.log(app.globalData.LoginStatus, "app.globalData.LoginStatus");

          app.globalData.LoginStatus = res.Data[0].loginstatus;
          app.globalData.UserID = res.Data[0].id;
          app.globalData.Phone = res.Data[0].phone;
          app.globalData.UserName = res.Data[0].nlikename;
          app.globalData.HeadPortrait = res.Data[0].headportrait;
          app.globalData.Status = res.Data[0].status;

          wx.setStorageSync("UserID", res.Data[0].id);


          if (app.globalData.Status == 2) {
            wx.reLaunch({
              url: '../../pages/scanPeople/scanPeople'
            })
          } else {
            wx.reLaunch({
              url: '../../pages/index/index'
            })
          }

        }
      })



    }


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '媛月幸福汇',
      path: '/pages/index/index?isShare=1',
      imageUrl: '/images/DefaultAvatar.png',
    }
  },
})